﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

using easyList.Entities;

namespace easyList.IO.Reader
{
    class XMLReader
    {
        static XMLReader instance = null;
        static readonly object padlock = new object();

        XMLReader() { }
        public static XMLReader Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new XMLReader();
                    }
                    return instance;
                }
            }
        }


        public ArchiveObject readXML(string sourcepath)
        {
            try
            {

                XmlTextReader reader = new XmlTextReader(sourcepath);

                ArchiveObject newArchive = new ArchiveObject();
                FileObject currentFileObject = new FileObject();
                string currentNode = "none";

                while (reader.Read())
                {

                    switch (reader.NodeType)
                    {

                        case XmlNodeType.Element: // The node is an element.
                            if (reader.Name == "File")
                            {
                                currentNode = "File";
                            }
                            if (reader.Name == "Filename")
                            {
                                currentNode = "Filename";
                            }
                            if (reader.Name == "StoreID")
                            {
                                currentNode = "StoreID";
                            }
                            if (reader.Name == "Size")
                            {
                                currentNode = "Size";
                            }
                            break;

                        case XmlNodeType.Text: //Display the text in each element.

                            if (currentNode == "Filename")
                            {
                                currentFileObject.filename = reader.Value;
                            }
                            if (currentNode == "StoreID")
                            {
                                currentFileObject.storeID = reader.Value;
                            }
                            if (currentNode == "Size")
                            {
                                currentFileObject.formattedSize = reader.Value;
                            }

                            break;

                        case XmlNodeType.EndElement: //Display the end of the element.
                            if (reader.Name == "File")
                            {
                                currentNode = "File";
                                newArchive.addFileObject(currentFileObject);
                                currentFileObject = new FileObject();
                            }
                            if (reader.Name == "Filename")
                            {
                                currentNode = "Filename";
                            }
                            if (reader.Name == "StoreID")
                            {
                                currentNode = "StoreID";
                            }
                            if (reader.Name == "Size")
                            {
                                currentNode = "Size";
                            }
                            break;
                    }

                }

                return newArchive;
            }

            catch (System.Xml.XmlException exc)
            {

                MessageBox.Show("Invalid XML" + exc.Message);

                return null;
            }
        }

    }
}
